/*
   @Copyright:LintCode
   @Author:   tjyemail
   @Problem:  http://www.lintcode.com/problem/space-replacement
   @Language: C++
   @Datetime: 16-02-09 05:18
   */

class Solution {
public:
	/**
	 * @param string: An array of Char
	 * @param length: The true length of the string
	 * @return: The true length of new string
	 * Tip: param string[] has enough space to store results
	 */
	int replaceBlank(char string[], int length) {
		// Write your code here
		int space=0, len;
		if (string==NULL) return 0;
		for(int i=0; i<length; ++i)
			space += (string[i]==' ');
		len = length + space * 2;
		string[len--] = '\0';
		for(int i=length-1; i>=0; --i){
			if (string[i]==' '){
				string[len--] = '0';
				string[len--] = '2';
				string[len--] = '%';
			}
			else string[len--]=string[i];
		}
		return strlen(string);
	}
};  
